package com.example.yls.demob;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

public class StuDbContentProvider extends ContentProvider {
    private StuDBOpenHelper stuDBOpenHelper;
    private static final String AUTOHORITY = "com.gdcp";
    private static UriMatcher uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);

    static {
        uriMatcher.addURI(AUTOHORITY, "student", 1);
        uriMatcher.addURI(AUTOHORITY, "score", 2);
    }
    @Override
    public boolean onCreate() {
        stuDBOpenHelper=new StuDBOpenHelper(getContext(),"stu.db",null,1);
        return true;
    }

    @Nullable
    @Override
    public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String orderBy) {
                     switch (uriMatcher.match(uri)) {
                         case 1:
                         SQLiteDatabase sqLiteDatabase = stuDBOpenHelper.getReadableDatabase();
                         Cursor cursor = sqLiteDatabase.query("student", projection, selection, selectionArgs, null, null, orderBy);
                                   return cursor;
                     }

                     return null;

    }

    @Nullable
    @Override
    public String getType(@NonNull Uri uri) {
        return null;
    }

    @Nullable
    @Override
    public Uri insert(@NonNull Uri uri, @Nullable ContentValues contentValues) {
             switch (uriMatcher.match(uri)){
                 case 1:
                     SQLiteDatabase sqLiteDatabase=stuDBOpenHelper.getWritableDatabase();
                     sqLiteDatabase.insert("student",null,contentValues);
                     sqLiteDatabase.close();
                     break;
             }



        return null;
    }

    @Override
    public int delete(@NonNull Uri uri, @Nullable String where, @Nullable String[] whereArgs) {
           switch (uriMatcher.match(uri)){
               case 1:
                   SQLiteDatabase sqLiteDatabase=stuDBOpenHelper.getWritableDatabase();
                   sqLiteDatabase.delete("student",where,whereArgs);
                   sqLiteDatabase.close();
                   break;
           }


        return 0;
    }

    @Override
    public int update(@NonNull Uri uri, @Nullable ContentValues contentValues, @Nullable String whereclause, @Nullable String[] whereArgs) {
           switch (uriMatcher.match(uri)){
               case 1:
                    SQLiteDatabase sqLiteDatabase=stuDBOpenHelper.getWritableDatabase();
                    sqLiteDatabase.update("student",contentValues,whereclause,whereArgs);
                    sqLiteDatabase.close();

                   break;
           }

        return 0;
    }
}
